package com.lagou.edu.service.impl;

import com.lagou.edu.repository.ResumeDao;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class ResumeServiceImpl implements ResumeService {
    // 要测试IOC哪个对象注入即可
    @Autowired(required = false)
    private ResumeDao resumeDao;

    @Override
    @Transactional(readOnly = true)
    public List<Resume> list() throws Exception {
        List<Resume> list = resumeDao.findAll();
        for (int i = 0; i < list.size(); i++) {
            Resume resume = list.get(i);
            System.out.println(resume);
        }
        return list;
    }

    @Override
    public Resume get(Long id) throws Exception {
        Optional<Resume> optional = resumeDao.findById(id);
        Resume resume = optional.get();
        System.out.println(resume);
        return resume;
    }

    @Override
    public void save(Resume resume) throws Exception {
        // 新增和更新都使用save方法，通过传入的对象的主键有无来区分，没有主键信息那就是新增，有主键信息就是更新
//        Resume resume = new Resume();
//        resume.setId(5l);
//        resume.setName("赵六六");
//        resume.setAddress("成都");
//        resume.setPhone("132000000");
        Resume save = resumeDao.save(resume);
        System.out.println(save);
    }

    @Override
    public void delete(Long id) throws Exception {
        resumeDao.deleteById(id);
    }
}
